package com.ocom.srvpaccess.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.master.EntityCompanyPart;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.redis.aspect.CacheRedisAspect;
import com.ocom.srvpaccess.dao.IDaoCompanyPart;
import com.ocom.srvpaccess.service.CompanyPartService;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CompanyPartImpl extends ServiceImpl<IDaoCompanyPart, EntityCompanyPart> implements CompanyPartService {

    @CacheRedisAspect(prefix="getCompanyDataSource",element={"companyId","type","isMaster"})
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public EntityCompanyPart getCompanyDataSource(Long companyId,Integer type,Integer isMaster) {
        QueryWrapper<EntityCompanyPart> companyPartQueryWrapper =new QueryWrapper<>();
        companyPartQueryWrapper.le("begin_com_id",companyId);
        companyPartQueryWrapper.eq("type",1);
        companyPartQueryWrapper.eq("is_master",isMaster);
        companyPartQueryWrapper.ge("end_com_id",companyId);
        companyPartQueryWrapper.last("limit 1");
        return this.getOne(companyPartQueryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public List<EntityCompanyPart> getDataSourceList() {
        QueryWrapper<EntityCompanyPart> companyPartQueryWrapper =new QueryWrapper<>();
        companyPartQueryWrapper.eq("type",1);
        companyPartQueryWrapper.eq("is_master",0);
        return this.list(companyPartQueryWrapper);
    }
}
